﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WenXia
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        GameState currentState = GameState.start;
        Texture2D texMouse;
        int width = 0;
        int height = 0;
        public GraphicsDeviceManager Graphics
        {
            get { return graphics; }
            set { graphics = value; }
        }
        DateTime timePauseStart;
        int millSecToPause;
        SpriteBatch spriteBatch;
        BackgroundManager bm;
        TextDrawer td;
        QuestionManager qm;
        MouseCom mc;
        TextContainer tcStart;
        TextContainer tcFPS;
        TextContainer tcCut12;
        TextContainer tcEnd;
        ButtonState mLLastState;
        TimeOutSprite rightSprite;
        TimeOutSprite wrongSprite;
        AnimatedSprite smurf1;
        AnimatedSprite smurf2;
        AnimatedSprite wenxia;
        public GameMain()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.GraphicsProfile = GraphicsProfile.Reach;
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //graphics.ToggleFullScreen();
            width = Window.ClientBounds.Width;
            height = Window.ClientBounds.Height;
            td = new TextDrawer(this);
            bm = new BackgroundManager(this);
            qm = new QuestionManager(this);
            mc = new MouseCom(this);
            smurf1 = new AnimatedSprite(this, "smurf1");
            smurf2 = new AnimatedSprite(this, "smurf2");
            wenxia = new AnimatedSprite(this, "character");
            smurf1.SetRegion(width - 100, height - 150, 100, 150);
            smurf2.SetRegion(width - 100, height - 150, 100, 150);
            wenxia.SetRegion(20, height - 200, 150, 350);
            rightSprite = new TimeOutSprite(this, "right");
            rightSprite.SetRegion(width / 2 - 100, height - 100,260,57);
            wrongSprite = new TimeOutSprite(this, "wrong");
            wrongSprite.SetRegion(width / 2 - 100, height - 100, 260,57);
            rightSprite.Visible = false;
            wrongSprite.Visible = false;
            bm.Visible = false;
            qm.Visible = false;
            //Components.Add(td);
            Components.Add(qm);
            Components.Add(bm);
            Components.Add(mc);
            Components.Add(rightSprite);
            Components.Add(wrongSprite);
            Components.Add(smurf1);
            Components.Add(smurf2);
            Components.Add(wenxia);
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            texMouse = Content.Load<Texture2D>("pointer");

            tcStart=new TextContainer(TextHelper.Paragraph(TextHelper.intro, 20), new Vector2(width / 2 - 270, height / 2 - 150));
            tcCut12 = new TextContainer(TextHelper.Paragraph(TextHelper.cut12, 20), new Vector2(width / 2 - 280, height / 2 - 200));
            tcEnd = new TextContainer(TextHelper.Paragraph(TextHelper.end, 20), new Vector2(width / 2 - 250, height / 2 - 150));
            tcFPS = new TextContainer();
            tcFPS.Position = new Vector2(0, 0);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            
        }
        public void Pause(int timeMillSec)
        {
            this.millSecToPause = timeMillSec;
            this.timePauseStart = DateTime.Now;
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (DateTime.Now.CompareTo(timePauseStart.AddMilliseconds(millSecToPause)) < 0)
                return;
            int width = Window.ClientBounds.Width;
            int height = Window.ClientBounds.Height;
            float fps = 1000f / gameTime.ElapsedGameTime.Milliseconds;
            tcFPS.Text = fps.ToString();
            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.Escape))
                this.Exit();
            MouseState ms = Mouse.GetState();
            mc.SetPos(ms.X, ms.Y);
            //td.Texts.Add(tcFPS);
            switch (currentState)
            {
                case GameState.start:
                    if (ms.LeftButton == ButtonState.Pressed)
                    {
                        currentState = GameState.scene1;
                        bm.Visible = true;
                        bm.BackgroundIndex = 0;
                        td.Visible = false;
                    }
                    td.Texts.Clear();
                    td.Texts.Add(tcStart);
                    break;
                case GameState.scene1:
                    qm.Visible = true;
                    if (qm.currentQuestion() == 0)
                        qm.SelectQuestion(0);
                    if (ms.LeftButton == ButtonState.Pressed&&mLLastState==ButtonState.Released)
                    {
                        AnswerResult ar=qm.GetAnswerResult(ms.X, ms.Y);
                        switch (ar)
                        {
                            case AnswerResult.Right:
                                qm.NextQuestion();
                                if (qm.currentQuestion() > 2)
                                {
                                    bm.Visible = false;
                                    currentState = GameState.cut12;
                                }
                                break;
                            case AnswerResult.Wrong:
                                wrongSprite.Show(1000);
                                break;
                            case AnswerResult.Unknown:
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                case GameState.cut12:
                    if (ms.LeftButton == ButtonState.Pressed&&mLLastState==ButtonState.Released)
                        currentState = GameState.scene2;
                    qm.Visible = false;
                    td.Texts.Clear();
                    td.Texts.Add(tcCut12);
                    td.TextColor = Color.White;
                    td.Visible = true;
                    break;
                case GameState.scene2:
                    bm.BackgroundIndex = 1;
                    bm.Visible = true;
                    qm.Visible = true;
                    qm.Ask();
                    if (ms.LeftButton == ButtonState.Pressed&&mLLastState==ButtonState.Released)
                    {
                        AnswerResult ar = qm.GetAnswerResult(ms.X, ms.Y);
                        switch (ar)
                        {
                            case AnswerResult.Right:
                                if (!qm.NextQuestion())
                                {
                                    bm.Visible = false;
                                    currentState = GameState.end;
                                }
                                break;
                            case AnswerResult.Wrong:
                                wrongSprite.Show(1000);
                                break;
                            case AnswerResult.Unknown:
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                case GameState.end:
                    qm.Visible = false;
                    td.TextColor = Color.White;
                    td.Texts.Clear();
                    td.Texts.Add(tcEnd);
                    td.Visible = true;
                    break;
                default:
                    break;
            }
            mLLastState = ms.LeftButton;
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            switch (currentState)
            {
                case GameState.start:
                   
                    
                    break;
                case GameState.scene1:
                    break;
                case GameState.scene2:
                    break;
                case GameState.end:
                    break;
                default:
                    break;
            }
            GraphicsDevice.Clear(Color.Black);            
            base.Draw(gameTime);
        }
    }
}
